<template>
  <div class="indexVideoManagement">
    <h3>首页推荐视频管理</h3>
    <div class="search">
      <div class="cond1 cond">
        <el-input placeholder="请输入视频标题" v-model="search1">
          <template slot="prepend">视频标题</template>
        </el-input>
      </div>
      <div class="cond1 cond">
        <el-input placeholder="请输入视频ID" v-model="search2">
          <template slot="prepend">视频ID</template>
        </el-input>
      </div>
      <div class="cond1 cond">
        <el-input placeholder="请输入用户id" v-model="search3">
          <template slot="prepend">用户id</template>
        </el-input> 
      </div>
      <div class="cond2 cond">
        <label>添加时间</label>
        <el-date-picker
          v-model="search4"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </div>
      <div class="cond2 cond">
        <label>付费类型</label>
        <el-select v-model="search5" placeholder="请选择">
          <el-option
            v-for="item in optionsPayType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div>
        <el-button type="primary" icon="el-icon-search" @click="searchMethod">搜索</el-button>
      </div>
    </div>
    <div class="opration">
      <el-button
        type="success"
        @click="addData"
        v-if="permissions_obj.id_132||permissions_obj.id_all"
      >添加</el-button>
      <el-button
        type="danger"
        @click="batchDelete"
        v-if="permissions_obj.id_131||permissions_obj.id_all"
      >批量移除</el-button>
      <el-button
        type="warning"
        @click="handeRelease"
        v-if="permissions_obj.id_136||permissions_obj.id_all"
      >发布</el-button>
    </div>
    <div class="list">
      <el-table
        border
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" header-align="center" align="center"></el-table-column>
        <el-table-column type="index" width="50" header-align="center" align="center" label="序号"></el-table-column>
        <el-table-column header-align="center" align="center" label="视频封面" width="100">
          <template slot-scope="scope">
            <div class="left" style="width: 80px; height: 80px">
              <img
                :src="scope.row.cover"
                alt
                style="max-width: 80px; max-height: 80px"
                @click="imgView(scope.row.cover)"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column header-align="center" align="center" label="视频信息" >
          <template slot-scope="scope">
            <div class="vedioBox">
              <div class="right">
                <div>ID：<span>{{scope.row.id}}</span></div>
                <div>标题：<span>{{scope.row.title}}</span></div>
                <div>
                  时长：
                  <span>{{scope.row.duration|sec_to_time}}</span>
                </div>
                <div v-if="scope.row.from>0" class="playBtnList">
                  <el-button
                    size="mini"
                    type="warning"
                    @click="handleBrowserPlay(scope.$index, scope.row)"
                  >播放正片</el-button>
                  <el-button
                    size="mini"
                    type="warning"
                    @click="handleBrowserPlayPreview(scope.$index, scope.row)"
                  >播放预览</el-button>
                </div>
                <div v-else class="playBtnList">
                  <el-button
                    size="mini"
                    type="warning"
                    @click="handlePlay(scope.$index, scope.row)"
                  >播放正片</el-button>
                   <el-button
                    size="mini"
                    type="warning"
                    @click="handlePlayPreview(scope.$index, scope.row)"
                  >播放预览</el-button>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="typeList"
          label="视频分类"
          width="120"
        ></el-table-column>
        <el-table-column header-align="center" align="center" label="付费类型" width="80">
          <template slot-scope="scope">
            <div>{{scope.row.video_paytype==2?scope.row.coins:""}}{{scope.row.video_paytype_zh}} </div>
          </template>
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="is_long_text"
          label="视频类型"
          width="80"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="created_at"
          label="上传时间"
          width="95"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="user_nick"
          label="用户名称"
          width="80"
        ></el-table-column>
        <el-table-column header-align="center" align="center" prop="uid" label="用户ID" width="80"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="like"
          label="点赞量"
          width="70"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="buy_video_count"
          label="购买量"
          width="70"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="video_perfect_created_at"
          label="添加时间"
          width="95"
        ></el-table-column>
        <el-table-column header-align="center" align="center" label="操作" width="90" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              v-if="permissions_obj.id_130||permissions_obj.id_all"
            >移除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page">
     <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage1"
        :page-sizes="[10,30,50,100]"
        :page-size="pageSize"
        layout="total, prev, pager, next,sizes,jumper"
        :total="totalSize"
      ></el-pagination>
    </div>
    <!-- //添加首页视频 -->
    <el-dialog title="添加首页视频" :visible.sync="addDialogVisible" width="40%" center>
      <div>输入视频id添加至首页，如有多个，请以半角逗号隔开</div>
      <div>如：555,555,555,555</div>
      <div>
        <el-input type="textarea" :rows="3" placeholder="请输入内容" v-model="textareaTopic"></el-input>
      </div>
      <div class="dialogBtn">
        <el-button type="primary" size="small" @click="submitAdd">提交</el-button>
        <el-button type="info" size="small" @click="cancelAdd">取消</el-button>
      </div>
    </el-dialog>
    <!-- //播放弹窗 -->
    <el-dialog title="播放" :visible.sync="playDialogVisible" width="40%" center>
      <div>
        <video
          controls
          width="100%"
          max-height="500"
          :src="playUrl"
        >Sorry, your browser doesn't support embedded videos.</video>
      </div>
    </el-dialog>
    <!-- //图片预览 -->
    <el-dialog :visible.sync="imgDialogVisible">
      <img width="100%" :src="imgPreview" alt />
    </el-dialog>
  </div>
</template>
<script>
import aes from "../../common/aes";
export default {
  name: "indexVideoManagement",
  data() {
    return {
      search1: "",
      search2: "",
      search3: "",
      search4: "",
      search5: "",
      //付费类型
      optionsPayType: [
        {
          value: "",
          label: "全部"
        },
        {
          value: 1,
          label: "VIP"
        },
        {
          value: 2,
          label: "钻石"
        },
        {
          value: 3,
          label: "免费"
        }
      ],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      tableData: [],
      currentPage1: 1,
      pageSize: 10,
      totalSize: 0,
      //多选框选中的值
      multipleSelection: [],
      //添加首页视频
      addDialogVisible: false,
      //视频编辑弹窗，新图片地址
      dialogImageUrl: "",
      //视频编辑弹窗，图片预览
      dialogVisible: false,
      //视频编辑弹窗，已选择类型
      checkList: [],
      //播放视频弹窗
      playDialogVisible: false,
      initData: {},
      //添加首页视频
      textareaTopic: "",
      //播放弹窗播放地址
      playUrl: "",
      imgDialogVisible: false,
      imgPreview: "",
      // 权限管理
      permissions: [],
      permissions_obj: {}
    };
  },
  created() {
    //权限判断
    this.permissions = this.$store.state.permissions;
    let oneOff = false;
    this.permissions.forEach((item, index) => {
      if (item == -1) {
        oneOff = true;
      } else {
        this.permissions_obj["id_" + item] = item;
      }
    });
    if (oneOff) {
      this.permissions_obj.id_all = true;
    } else {
      this.permissions_obj.id_all = false;
    }
  },
  mounted() {
    this.getDataList();
  },
  methods: {
    imgView(url) {
      this.imgDialogVisible = true;
      this.imgPreview = url;
    },
    // 分页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage1 = val;
      this.getDataList();
    },
     handleSizeChange(val){
      this.pageSize=val;
      this.getDataList();
    },
    searchMethod() {
      this.currentPage1 = 1;
      this.getDataList();
    },
    //多选框选中
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
    },
    addData() {
      this.addDialogVisible = true;
    },
    //批量移除
    batchDelete() {
      if (this.multipleSelection.length < 1) {
        this.$message({
          message: "请选取删除内容",
          type: "warning"
        });
        return;
      }
      this.$confirm("是否继续该批量操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.batchDeleteMethod();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消批量删除"
          });
        });
    },

    //删除
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该行数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteMethod(row.video_id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },

    //提交添加
    async submitAdd() {
      let parames = {
        handshake: "",
        data: {
          ids: this.textareaTopic
        }
      };
      let res = await this.$axios.post(
        this.$api.videoIndexAdds,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.addDialogVisible = false;
        this.getDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //取消添加哦哦
    cancelAdd() {
      this.addDialogVisible = false;
    },
    //播放弹窗正片
    handlePlay(index, row) {
      this.playDialogVisible = true;
      this.playUrl = row.mu;
    },
    //播放弹窗预览
    handlePlayPreview(index, row){
      this.playDialogVisible = true;
      this.playUrl = row.smu;
    },
    //浏览器播放正片
    handleBrowserPlay(index, row) {
      window.open(row.mu, "_blank", "width=375,height=667,left=500,top=100");
    },
    //浏览器播放预览
    handleBrowserPlayPreview(index, row){
      window.open(row.smu, "_blank", "width=375,height=667,left=500,top=100");
    },
    //获取初始化数据列表
    async getDataList() {
      let parames = {
        handshake: "",
        data: {
          page: this.currentPage1,
          page_size: this.pageSize,
          search: {
            title: this.search1,
            video_id: this.search2,
            user_id: this.search3,
            stime:
              this.search4 && this.search4.length > 0 ? this.search4[0] : "",
            otime:
              this.search4 && this.search4.length > 1 ? this.search4[1] : "",
            video_paytype: this.search5
          }
        }
      };
      let res = await this.$axios.post(
        this.$api.videoIndexList,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.initData = res.data;
        this.initData.data.forEach((item, index) => {
          //图片进行aes解密
          let _promise = new Promise((resolve, reject) => {
            aes.getDecrypt(item.cover, resolve);
          });
          _promise.then(cover => {
            item.cover = cover;
          });
          if (item.check == 0) {
            item.check_text = "未审核";
          } else if (item.check == 1) {
            item.check_text = "已审核";
          }
          if (item.status == 0) {
            item.status_text = "下架";
          } else if (item.status == 1) {
            item.status_text = "上架";
          } else if (item.status == 2) {
            item.status_text = "转码中";
          } else if (item.status == 3) {
            item.status_text = "转码失败";
          }
          if (item.is_long == 1) {
            item.is_long_text = "长视频";
          } else if (item.is_long == 0) {
            item.is_long_text = "短视频";
          }
          let typeList = [];
          item.category.forEach((it, ind) => {
            typeList.push(it.title);
          });
          item.typeList = typeList.join(",");
        });
        this.totalSize = this.initData.total;
        this.tableData = this.initData.data;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //删除接口
    async deleteMethod(id) {
      let parames = {
        handshake: "",
        data: {
          video_id: id.toString()
        }
      };
      let res = await this.$axios.post(
        this.$api.videoIndexDelete,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.getDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //批量删除接口
    async batchDeleteMethod() {
      let arr = [];
      this.multipleSelection.forEach((item, index) => {
        arr.push(item.video_id);
      });
      let parames = {
        handshake: "",
        data: {
          ids: arr.join(",")
        }
      };
      let res = await this.$axios.post(
        this.$api.videoIndexDelete_more,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.getDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //发布
    handeRelease() {
      this.$confirm("确定继续该操作吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.releaseMethod();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //发布接口
    async releaseMethod() {
      let parames = {
        handshake: "",
        data: {
          type:1
        }
      };
      let res = await this.$axios.post(
        this.$api.videoHome,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    }
  }
};
</script>
<style scoped>
.userVideoUploadManagement {
  padding-bottom: 0.2rem;
}
h3 {
  padding: 0.2rem;
  color: #3c763d;
}
.search {
  display: flex;
  margin-top: 0.2rem;
  padding-left: 0.2rem;
  width: 100%;
  flex-wrap: wrap;
}
.el-input {
  width: 2.5rem;
}
.search label {
  width: 0.83rem;
  height: 0.4rem;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
  color: #909399;
  border: 1px solid #dcdfe6;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  font-size: 14px;
}
.cond {
  display: flex;
  margin-right: 0.4rem;
  margin-bottom: 0.1rem;
}
.search .el-select >>> .el-input__inner {
  border-left: none;
  border-top-left-radius: 0px;
  border-bottom-left-radius: 0px;
}
.search .el-select {
  width: 1.67rem;
}
.opration {
  margin-top: 0.2rem;
  padding-left: 0.2rem;
}
.page {
  margin-top: 0.2rem;
  display: flex;
  justify-content: center;
}
.list {
  margin-top: 0.2rem;
  padding: 0 0.1rem;
  font-size: 0.12rem;
}
.dialogBtn {
  display: flex;
  justify-content: center;
  margin-top: 0.3rem;
}
.row {
  margin-bottom: 0.1rem;
  display: flex;
  align-items: center;
}
.row span {
  width: 1rem;
  display: block;
}
.row >>> .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.row >>> .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.row >>> .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.row >>> .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.el-checkbox-group {
  width: 90%;
}
.tagList {
  width: 100%;
  padding: 0 0.2rem;
}
.tagList ul {
  display: flex;
  flex-wrap: wrap;
}
.tagList li {
  background: rgb(26, 213, 226);
  padding: 5px 10px;
  margin-right: 0.1rem;
  color: #fff;
  border-radius: 3px;
  cursor: pointer;
}
.tagList li:hover {
  background: rgb(25, 168, 179);
}
.tagList .active {
  background: rgb(25, 168, 179);
}
.vedioBox {
  display: flex;
  align-items: center;
}
.vedioBox .right {
  display: flex;
  justify-content: flex-start;
  flex-direction: column;
  align-items: flex-start;
  padding-left: 0.05rem;
}
.vedioBox .right .playBtnList {
  margin-top:.15rem;
}
.vedioBox .right span{
  color:rgb(19, 141, 150);
}
.el-button {
  margin-bottom: 10px;
}
</style>